<?php

/**
 * uniBoard API Ban Filter
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_ban extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Ban ID
	 *
	 * @var integer
	 */
	
	public $ban_id 				= 0;
	
	/**
	 * Banned User ID
	 *
	 * @var integer
	 */
	
	public $user_id 				= 0;
	
	/**
	 * Banned User Name
	 *
	 * @var string
	 */
	
	public $user_name 				= '';
	
	/**
	 * Banned IP Address
	 *
	 * @var string
	 */
	
	public $ban_ip 				= '';
	
	/**
	 * Banned E-Mail Address
	 *
	 * @var string
	 */
	
	public $ban_email 				= '';
	
	/**
	 * Admin-Side Ban Reason
	 *
	 * @var string
	 */
	
	public $ban_reason_admin		= '';
	
	/**
	 * User-Side Ban Reason
	 *
	 * @var string
	 */
	
	public $ban_reason_user		= '';

	/**
	 * Ban Date
	 *
	 * @var integer
	 */
	
	public $ban_date 				= 0;
		
	/**
	 * Ban Lenght (minutes)
	 *
	 * @var integer
	 */
	
	public $ban_lenght				= 0;
					
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Ban ID
		$this -> ban_id		= (integer) $target_id;
		
		// Autoload If Ban ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT b.*, u.user_name
			FROM ' . DB_PREFIX . 'ban_filters b
			LEFT JOIN ' . DB_PREFIX . 'users u ON b.user_id = u.user_id
			WHERE b.ban_id = \'' . $this -> ban_id . '\'
			LIMIT 1');
			
			// Ban found
			if ( $ban_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> user_id		 			= $ban_result['user_id'];
				$this -> user_name		 			= $ban_result['user_name'];
				$this -> ban_ip	 					= $ban_result['ban_ip'];
				$this -> ban_email	 				= $ban_result['ban_email'];
				$this -> ban_reason_admin	 		= $ban_result['ban_reason_admin'];
				$this -> ban_reason_user 			= $ban_result['ban_reason_user'];
				$this -> ban_date 					= $ban_result['ban_date'];
				$this -> ban_lenght 				= $ban_result['ban_lenght'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_ban_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> ban_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'ban_filters');
			
				// Store new ban id
				$this -> ban_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'ban_filters', 'ban_id = \'' . $this -> ban_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'bans');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'ban_filters', 'ban_id = \'' . $this -> ban_id . '\'');
		
		// Flush cache
		class_cache::flushCache( 'bans');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set user_id
	 *
	 * @param integer $new_value
	 */
	
	function setUserId( $new_value){
		
		// Store in object
		$this -> user_id = (integer) $new_value;
		
		// Add to made changes
		$this -> changes['crawler_name'] = $this -> user_id;

	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set ban_ip
	 *
	 * @param string $new_value
	 */
	
	function setBanIp( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> ban_ip = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_ban_ip_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['ban_ip'] = $this -> appCore -> db -> strEscape( $this -> ban_ip);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_ban_ip_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set ban_email
	 *
	 * @param string $new_value
	 */
	
	function setBanEmail( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> ban_email = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_ban_email_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['ban_email'] = $this -> appCore -> db -> strEscape( $this -> ban_email);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_ban_email_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set ban_reason_admin
	 *
	 * @param string $new_value
	 */
	
	function setBanReasonAdmin( $new_value){
		
		// Store in object
		$this -> ban_reason_admin = $new_value;
			
		// Add to made changes
		$this -> changes['ban_reason_admin'] = $this -> appCore -> db -> strEscape( $this -> ban_reason_admin);
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set ban_reason_admin
	 *
	 * @param string $new_value
	 */
	
	function setBanReasonUser( $new_value){
		
		// Store in object
		$this -> ban_reason_user = $new_value;
			
		// Add to made changes
		$this -> changes['ban_reason_user'] = $this -> appCore -> db -> strEscape( $this -> ban_reason_user);
		
	}

	// ------------------------------------------------------------------------

	/**
	 * Set ban_date
	 *
	 * @param integer $new_value
	 */
	
	function setBanDate( $new_value){
		
		// Store in object
		$this -> ban_date = (integer) $new_value;
		
		// Add to made changes
		$this -> changes['ban_date'] = $this -> ban_date;

	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set ban_lenght
	 *
	 * @param integer $new_value
	 * @param string $new_date
	 */
	
	function setBanLenght( $new_value, $new_date = ''){
		
		// Ban Filter expired?
		if ( isset( $new_date[1]) && isset( $new_date[10]) && !isset( $new_date[11]))
		{
			// Explode ban
			$new_value = explode( '-', $new_date);
			
			// Date allright?
			if ( isset( $new_value[2]) && !isset($new_value[3]))
			{
				// Make Expire Date
				$this -> ban_lenght = $this -> appCore -> getTimestamp( $new_value[0], $new_value[1], $new_value[2]);
			}
			else
			{
				// Expire date incorrect
				$this -> setError( $appCore -> lang['acp_ban_expire_incorrect']);
			}
		}
		else
		{
			// Store in object
			$this -> ban_lenght = (integer) $new_value;
			
			// Add our stard date to lenght
			$this -> ban_lenght += $this -> ban_date;
		}
				
		// Minimal Value?
		if ( $this -> ban_lenght > 0 && $this -> ban_lenght <= $this -> appCore -> time)
		{
			$this -> setError( $appCore -> lang['acp_ban_expire_incorrect']);
		}
		else
		{
			// Add to made changes
			$this -> changes['ban_lenght'] = $this -> ban_lenght;
		}
		
	}
			
}